package com.sduept.nwld.dataserver.manager.message;

import com.sduept.core.dao.SuperDAO;
import com.sduept.message.entity.MessageSender;
import com.sduept.message.observer.MessageConfiguration;
import com.sduept.nwld.dataserver.model.library.MessageReceiver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Author
 * @version V1.0
 * @Date 2018年8月15日 下午2:05:26
 */
@Service
public class MessageReceiverManager {

	@Autowired
	private SuperDAO dao;

	/**
	 * 保存
	 * 
	 * @param bean
	 * @return
	 */
	public MessageReceiver save(MessageReceiver bean) {
		return dao.createOrUpdate(bean, MessageReceiver.class);
	}

	/**
	 * 根据Id查找
	 * 
	 * @param id
	 * @return
	 */
	public MessageReceiver getById(String id) {
		return dao.findById(id, MessageReceiver.class);
	}

	/**
	 * 根据id查找description
	 * 
	 * @param id
	 * @return
	 */
	public String getDescriptionById(String id) {
		MessageReceiver m = dao.findById(id, MessageReceiver.class);
		return m.getName();
	}

	/**
	 * 根据Id删除
	 * 
	 * @param id
	 */
	public void deleteById(String id) {
		dao.delete(id, MessageReceiver.class);
	}

	@Inject
	private MessageConfiguration messageConfiguration;

	/**
	 * 查找全部
	 * 
	 * @return
	 */
	public List<MessageReceiver> listAll() {
		List<MessageSender> senderList = messageConfiguration.getMessageSenders();
		List<MessageReceiver> receiverList = new ArrayList<MessageReceiver>();
		for (MessageSender ms : senderList) {
			MessageReceiver mr = new MessageReceiver();
			mr.setId(ms.getId());
			mr.setName(ms.getName());
			mr.setDescription(ms.getDescipition());
			receiverList.add(mr);
		}
		return receiverList;
	}

	/**
	 * 根据Id的数组查询
	 * 
	 * @param ids
	 * @return
	 */
	public List<MessageReceiver> listByIds(String[] ids) {
		return dao.findByConditionIN("id", ids, MessageReceiver.class);

	}
}
